<template>
    <!-- form 查询表单 -->
    <el-form :model="book" ref="formRef" label-width="auto" label-position="right" style="padding-bottom: 10px;">
        <el-row :gutter="20">
            <el-col :span="8">
                <el-form-item label="书名" prop="name">
                    <el-input v-model="book.name" placeholder="请输入书名" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="作者" prop="author">
                    <el-input v-model="book.author" placeholder="请输入作者" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="状态" prop="state">
                    <el-select v-model="book.state" placeholder="书籍状态" clearable>
                        <el-option label="全部" value="" />
                        <el-option label="在馆" value="1" />
                        <el-option label="已借出" value="0" />
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="24">
                <el-button type="primary" @click="doList">查询</el-button>
                <el-button type="default" @click="resetForm(formRef)">重置</el-button>
                <el-button type="success" @click="showAddDialog">新增</el-button>
            </el-col>
        </el-row>
    </el-form>

    <!-- table 表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="index" lable="序号" width="80"/>
        <el-table-column prop="id" label="ID"/>
        <el-table-column prop="name" label="书名" />
        <el-table-column prop="author" label="作者" />
        <el-table-column prop="date" label="出版日期" />
        <el-table-column prop="press" label="出版社" />
        <el-table-column prop="price" label="价格" sortable/>
        <el-table-column prop="state" label="状态">
            <template #default="scope">
                <el-tag v-if="scope.row.state == 1" type="success">在馆</el-tag>
                <el-tag v-else type="danger">已借出</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
            <template #default="scope">
                <el-button type="primary" @click="showDetailPage(scope.row)">详情</el-button>
                <el-button type="warning" @click="showUpdateDialog(scope.row)">更新</el-button>
                <el-button type="danger" @click="doDelete(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 新增 dialog-->
    <el-dialog v-model="addDialogVisible" title="新增书籍" width="40%" top="2vh" destroy-on-close>
        <add-page
            @flushPage="doFlushPage"
            @closeDialog="addDialogVisible = false">
        </add-page>
    </el-dialog>

    <!-- 更新 dialog-->
    <el-dialog v-model="updateDialogVisible" title="更新书籍" width="40%" top="2vh" destroy-on-close>
        <update-page ref="updatePageRef"
             @flushPage="doFlushPage"
             @closeDialog="updateDialogVisible = false">
        </update-page>
    </el-dialog>

    <!-- 详情 页面 -->
    <detail-page ref="detailPageRef"></detail-page>
</template>

<script setup lang="ts">
import {ref, onMounted, nextTick} from 'vue'
import {ElMessage, FormInstance} from "element-plus";
import axios from 'axios'
import DetailPage from './DetailPage.vue';
import AddPage from './AddPage.vue';
import UpdatePage from './UpdatePage.vue';

const formRef = ref<FormInstance>();
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

// form 查询表单绑定的数据对象：book
const book = ref({
    name:'',
    author:'',
    state:''
});

// table 数据表格绑定的数据对象：tableData
const tableData = ref([]);
// 查询
const doList = async () => {
    const {data} = await axios.get('/api/book',{params:book.value});
    tableData.value = data;
}

// 删除
const doDelete = async (id)=>{
    const {data} = await axios.delete('/api/book/'+id);
    if(data > 0){
        doFlushPage();		// 删除成功，刷新表格数据
        ElMessage.success('删除成功');
    }else{
        ElMessage.error('删除失败');
    }
}

// 刷新页面
const doFlushPage = ()=>{
    resetForm(formRef.value);
    doList();
}

// 初始化
onMounted(()=>{
    doList();   // 查询数据
})
//--------------------------- 新增 对话框 ---------------------------
const addDialogVisible = ref(false);
const showAddDialog = ()=>{
    addDialogVisible.value = true;
}

//--------------------------- 更新 对话框 ---------------------------
const updateDialogVisible = ref(false);
const updatePageRef = ref(); // 组件引用
const showUpdateDialog = (obj)=>{
    updateDialogVisible.value = true;
    nextTick(()=>{
        updatePageRef.value.handleUpdateData(obj);
    })
}

//--------------------------- 详显 页面---------------------------
// 详显组件引用 -> 绑定到
const detailPageRef = ref();
const showDetailPage = (obj)=>{
    detailPageRef.value.handleDetailData(obj);
}
</script>